package com.lsbc.pay.config;

import java.nio.file.AccessDeniedException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.lsbc.pay.common.ApiResponse;
import com.lsbc.pay.common.BizException;

/**
 *  异常处理
 * @author yiyang 
 * @date 2019年12月7日
 */
@SuppressWarnings("rawtypes")
@RestControllerAdvice
public class ExceptionHandlerAdvice {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
    private static final String ERROR_MSG = "服务器开小差";

    
	@ExceptionHandler({IllegalArgumentException.class})
	@ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse badRequestException(IllegalArgumentException exception) {
		LOGGER.error("illegalArgumentException:{}",exception);
    	return ApiResponse.of(HttpStatus.BAD_REQUEST.value(), ERROR_MSG);
    }

	@ExceptionHandler({ AccessDeniedException.class})
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public ApiResponse badRequestException(AccessDeniedException exception) {
		LOGGER.error("reason:{},exception:{}",exception.getReason(),exception);
		return ApiResponse.of(HttpStatus.BAD_REQUEST.value(), ERROR_MSG);
	}
	
	@ExceptionHandler({NoHandlerFoundException.class})
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public ApiResponse NotFoundException(NoHandlerFoundException exception){
		LOGGER.error("method:{},noHandlerFoundException:{}",exception.getHttpMethod());
		return ApiResponse.of(HttpStatus.NOT_FOUND.value(), ERROR_MSG);
	}

    @ExceptionHandler({HttpMessageNotReadableException.class,
            UnsatisfiedServletRequestParameterException.class, MethodArgumentTypeMismatchException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse badRequestException(Exception exception) {
    	LOGGER.error("illegalArgumentException:{}",exception);
    	return ApiResponse.of(HttpStatus.BAD_REQUEST.value(), ERROR_MSG);
    }
    
    @ExceptionHandler({MissingServletRequestParameterException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResponse missingServletRequestParameter(MissingServletRequestParameterException e) {
    	LOGGER.error("parameterName:{},parameterType:{}，exception:{}",e.getParameterName(),e.getParameterType(),e);
    	return ApiResponse.of(HttpStatus.INTERNAL_SERVER_ERROR.value(), ERROR_MSG);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public ApiResponse methodNotSupportHandle(HttpRequestMethodNotSupportedException e) {
    	LOGGER.error("method:{}，httpRequestMethodNotSupportedException:{}",e.getMethod(),e);
    	return ApiResponse.of(HttpStatus.METHOD_NOT_ALLOWED.value(), ERROR_MSG);
    }
    
    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResponse exception(Throwable throwable) {
        LOGGER.error(ERROR_MSG, throwable);
        return ApiResponse.of(HttpStatus.INTERNAL_SERVER_ERROR.value(), ERROR_MSG);
    }
    
    @ExceptionHandler(BizException.class)
    public ApiResponse bizException(BizException throwable) {
        LOGGER.error(ERROR_MSG, throwable);
        return ApiResponse.of(throwable.getCode(), throwable.getMessage());
    }

}
